<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>用户新增/编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../js/formSelects/formSelects-v4.css">
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm">
        <input type="hidden" name="userType" value="0">
        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label"><span style="color: red;">*</span>账号</label>
            <div class="layui-input-block">
                <input type="text" id="iptuserId" name="userId" lay-verify="required|strOrNum|min3"  required autocomplete="off" placeholder="登录用户账号"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label"><span style="color: red;">*</span>用户名</label>
            <div class="layui-input-block">
                <input type="text" id="iptuserName" name="userName" lay-verify="required" required placeholder="用户名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label"><span style="color: red;">*</span>性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="男" title="男" checked="checked">
                <input type="radio" name="sex" value="女" title="女">
            </div>
        </div>

        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select id="role" name="roleIds" xm-select="role" >
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <select id="dept" name="deptId">
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-block">
                <input type="text" name="code" placeholder="身份证号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="mail" lay-verify="mailnull" placeholder="电子邮箱地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-block">
                <input type="tel" name="phone" lay-verify="phonenull" placeholder="联系手机" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remarks" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" id="btnSave" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    })
        .extend({
            formSelects: 'formSelects/formSelects-v4'
        });
    layui.use(['jquery','form','validate','common','formSelects','upload'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            validate = layui.validate,
            formSelects = layui.formSelects,
            upload = layui.upload,
            common = layui.common;
        validate.regVerify();//注册验证
        var id = common.getUrlParam("id");
        var type = common.getUrlParam("type");
        if (!type) type="add";  //默认为add

        //自定义返回数据中name的key, 默认 name
        formSelects.config("role",{
            keyName: 'name',
            keyVal: 'code'})
            .data("role","server",{
            url: './common/list4Select?type=role',
            beforeSuccess: function(id, url, searchVal, result){
                //我要把数据外层的code, msg, data去掉
                result = result.data;
                //然后返回数据
                return result;
            },success: function(id, url, searchVal, result){
                loadData();
            }
        });

        common.initSelectData("#dept","./common/deptTree4Select");


        var loadData = function(){
            if (type=="add"){
                return;
            }
            $("#iptuserId").attr("readonly","readonly");
            //加载数据
            //获取用户信息
            $.ajax({
                url: "user/info",
                dataType: "json",
                data: { id: id },
                success: function (rtn) {
                    if (rtn && rtn.code===0) {
                        var data = rtn.data;
                        form.val('dataForm',{
                            userId:data.userId,
                            userName:data.userName,
                            sex:data.sex,
                            roleIds:data.roleIds,
                            deptId:data.deptId,
                            mail:data.mail,
                            userType:data.userType,
                            phone:data.phone,
                            code:data.code,
                            remarks:data.remarks
                        });
//                        if(data.pic){
//                            $("#imgPreview").attr("src","../upload/head/"+data.pic);
//                        }
                        formSelects.value('role',data.roleIds);
                    }
                }
            });
        }

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            //common.consoleDebug(JSON.stringify(data.field));
            //上传图片
            $.ajax({
                type: 'post',
                url: 'user/'+(type=="edit"?"edit":"add"),
                data: data.field,
                success: function(rtn){
                    if (rtn.code===0) {  //数据保存成功
                        var msg ="保存成功!";
                        if (type=="add"){
                            msg = "保存成功,新用户默认密码为:"+rtn.data;
                        }
                        common.showInfo(msg,function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });
</script>
</body>
</html>